//
//  HWGiftBagViewController.m
//  HWanGame
//
//  Created by Chendaobin on 2019/8/23.
//  Copyright © 2019年 Chendaobin. All rights reserved.
//

#import "HWGiftBagViewController.h"
#import "HWDailyCheckController.h"  // 每日签到
#import "HWLoginController.h"
#import "HWOpenClothingViewController.h"
#import "HWTaskCenterViewController.h" // 任务中心界面
#import "HWPaiHangBangViewController.h" // 排行榜
#import "HWIntegralMallViewController.h" // 积分商城
#import "HWCustomerServiceCenterViewController.h" // 客服中心
#import "HWInviteFriendsViewController.h" //邀请好友
#import "HWLiBaoCenterViewController.h" // 礼包中心
#import <SafariServices/SafariServices.h>
#import "XMGWebViewController.h"

#import "HWGiftBagCenterzhongxingViewController.h" // 最新礼包中心




@interface HWGiftBagViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(strong,nonatomic) UITableView *mainaTableVie;
@property(strong,nonatomic) UIView *hederView;
@property(strong,nonatomic) UIImageView *guagaoimageView;
@property(strong,nonatomic) UIView *jifenView;
@property(strong,nonatomic) NSArray *dataArr;
@property(strong,nonatomic) NSMutableArray *imageMuArr;
@property(strong,nonatomic) NSMutableArray *labelMuArr;


@end

@implementation HWGiftBagViewController

-(UITableView *)mainaTableVie {
    
    if  (_mainaTableVie == nil) {
        
        _mainaTableVie = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH) style:UITableViewStylePlain];
        _mainaTableVie.delegate = self;
        _mainaTableVie.dataSource = self;
        _mainaTableVie.separatorStyle = UITableViewCellSeparatorStyleNone;
        _mainaTableVie.backgroundColor = [UIColor whiteColor];
    }
    return   _mainaTableVie ;
    
}

-(void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    [self chaxunfulie];
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor redColor];
    
    // 设置常用属性
    [self commonAttributes];
    // 设置UI
    [self setUpUI];
    // tian添加头部
    [self addheaderView];
    
    //[self chaxunfulie];
    // meiridatiico 
}


// 设置常用的属性
- (void)commonAttributes {
    
    self.navigationItem.title = @"福利中心";
    
    
    
    
}


- (void)chaxunfulie {
    
       NSString *uidStr = [NSString stringWithFormat:@"%@",[HWLocallyStoredData getLoginUserID]];
//        NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
//        NSString *signStr = [NSString stringWithFormat:@"%@%@%@",uidStr,tStr,@"fsd213ewdsadqwe2121213edsad"];
//        NSString  *signStrMd5  =   [WMUtil md5HexDigest:signStr];
//
        
          
        
      NSDictionary *parametes = @{
                                                @"ios" : @"1",
                                                @"ct"  : @"app",
                                                @"ac" : @"welfare",
                                                @"uid" :uidStr
        
                                               };
      
      
        
        
  
        
        
      //  [MBProgressHUD show:@"正在签到...."];
        [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
        

           NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
               if ([codeStr intValue] == 1) {
              
                //  [MBProgressHUD showError:response[@"message"]];
                   self.dataArr = response[@"data"];
                   
                   for (int i = 0; i < self.dataArr.count; i++) {
                       UIImageView *imageView = self.imageMuArr[i];
                       UILabel *labe = self.labelMuArr[i];
                       NSDictionary *dic =  self.dataArr[i];
                       NSURL *url = [NSURL URLWithString: dic[@"icon"]];
                       [imageView sd_setImageWithURL:url placeholderImage:nil];
                       labe.text = [NSString stringWithFormat:@"%@",dic[@"name"]];
                       
                       
                   }
                   
                   
              
                } else {
        
                    [MBProgressHUD showError:response[@"message"]];
        
                }
        
        
        
          } failure:^(NSString *error) {
                // [SVProgressHUD dismiss];
        
                        [MBProgressHUD showSuccess:@"查询失败"];
               
        
        
         }];
      
      
    
    
}










// 设置UI
- (void)setUpUI {
    
    
    [self.view addSubview:self.mainaTableVie];
    UIView *hederview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 700)];
    //   hederview.backgroundColor  = [UIColor redColor];
    self.hederView = hederview;
    self.mainaTableVie.tableHeaderView = hederview;
 
}

// 给tabview 添加头部
- (void)addheaderView {
    self.guagaoimageView = [[UIImageView alloc]init];
    self.guagaoimageView.frame = CGRectMake(0, 0, kScreenW, 138*CFLScaleValue);
    self.guagaoimageView.image = [UIImage imageNamed:@"welfare_banner"];
    [self.hederView addSubview: self.guagaoimageView];
    
    
    self.jifenView  = [[UIView alloc]initWithFrame:CGRectMake(15, 138*CFLScaleValue+20, kScreenW-30, 110)];
    self.jifenView.backgroundColor = [UIColor whiteColor];
    self.jifenView.layer.shadowColor = [UIColor colorWithRed:113/255.0 green:152/255.0 blue:255/255.0 alpha:0.49].CGColor;
    self.jifenView.layer.shadowOffset = CGSizeMake(0,0);
    self.jifenView.layer.shadowOpacity = 1;
    self.jifenView.layer.shadowRadius = 12;
    self.jifenView.layer.cornerRadius = 12;
    self.jifenView.layer.masksToBounds = YES;
    self.jifenView.clipsToBounds = NO;
    [self.hederView addSubview: self.jifenView];
    
     NSArray  *listArr = @[@"每日签到",@"积分商城",@"任务中心"];
    NSArray  *icoArr = @[@"welfare_signin",@"welfare_integral",@"welfare_mission"];
    CGFloat hengjianju = (kScreenW - 30-3*60)/6;
   // CGFloat  shujianju = (166-100)/3;
    
    for (int i = 0 ; i < listArr.count; i++) {
        int shulian = (i*2+2) -1;
        
        
        UIButton *butt  = [UIButton setButtonWithFrame:CGRectMake(i*60+hengjianju*shulian, 17, 60, 75) Title:listArr[i] Image:icoArr[i] Font:13 Color:RGB(51, 51, 49) Target:self Selector:@selector(buttonTap:)];
        butt.tag = i;
        [butt layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleTop imageTitleSpace:12];
        // butt.backgroundColor = [UIColor redColor];
        [self.jifenView addSubview:butt];
        
    }
    
    
    
    
    
    
    
    UIView *zuixiamianview = [[UIView alloc]initWithFrame:CGRectMake(0, self.jifenView.bottom+30, kScreenW, (35*CFLScaleValue+13+10)*4+20)];
   // zuixiamianview.backgroundColor = [UIColor redColor];
    
    [self.hederView addSubview:zuixiamianview];

    NSArray *fanlilistArr = @[@"申请返利",@"排行榜",@"礼包中心",@"联系客服",@"开服表",@" 邀请好友"];
    NSArray   *paihangbangcoArr = @[@"welfare_l1",@"welfare_l2",@"welfare_l3_libao",@"welfare_l4",@"welfare_l5",@"welfare_l6"];
    CGFloat hengjianjuLiin = (kScreenW-  (35*CFLScaleValue+25)*3)/6;
    self.imageMuArr = [[NSMutableArray alloc]init];
   self.labelMuArr = [[NSMutableArray alloc]init];
    for (int i = 0 ; i < 12; i++) {
    
        int row = i/3;
        int line = i%3;
        int shulian = (line*2+2) -1;

        UIView *redview = [[UIView alloc]initWithFrame:CGRectMake(hengjianjuLiin*shulian+line*(35*CFLScaleValue+25), row*(35*CFLScaleValue+13+10)+row*20, 35*CFLScaleValue+25, 35*CFLScaleValue+13+10)];
      //  redview.backgroundColor = [UIColor orangeColor];
        [zuixiamianview addSubview:redview];
        
        
        UIImageView *imview = [[UIImageView alloc]initWithFrame:CGRectMake(12, 0,  35*CFLScaleValue, 35*CFLScaleValue)];
     //   imview.image = [UIImage imageNamed:paihangbangcoArr[i]];
        imview.userInteractionEnabled = YES;
        imview.tag = i;
        
        [redview addSubview:imview];
        
        [self.imageMuArr addObject:imview];
        
        UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imviewTaKaifu:)];
        [imview addGestureRecognizer:tap1];
        
       // UILabel
        UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 35*CFLScaleValue+10, 35*CFLScaleValue+25, 13)];
      //  nameLabel.text = fanlilistArr[i];
        nameLabel.textAlignment = NSTextAlignmentCenter;
        nameLabel.font = [UIFont systemFontOfSize:13];
        nameLabel.textColor = RGB(51, 51, 49);
        [redview addSubview:nameLabel];
        [self.labelMuArr addObject:nameLabel];
        
    
    }
    
    
    
}




- (void)buttonTap:(UIButton *)sender {
    
    
    
    
    
    
    
    if(sender.tag == 0) {
        
        if ([[HWLocallyStoredData getLoginUserID] isEqualToString:@""]) {
                     HWLoginController  *login = [[HWLoginController alloc]init];
                       [self.navigationController pushViewController:login animated:YES];
                      return;
               
        }
              
        
        HWDailyCheckController *Daily = [[HWDailyCheckController alloc]init];
        [self.navigationController pushViewController:Daily animated:YES];
        
        
    } else if(sender.tag == 1) {
        
//        HWLoginController *logview = [[HWLoginController alloc]init];
//        [self.navigationController pushViewController:logview animated:YES];
        
        HWIntegralMallViewController *integral = [[HWIntegralMallViewController alloc]init];
        [self.navigationController pushViewController:integral animated:YES];
        
        
        
    } else {
        
        if ([[HWLocallyStoredData getLoginUserID] isEqualToString:@""]) {
                
            HWLoginController  *login = [[HWLoginController alloc]init];
            [self.navigationController pushViewController:login animated:YES];
                             return;
                      
        }
                    
        
        HWTaskCenterViewController *taskCenter = [[HWTaskCenterViewController  alloc]init];
        [self.navigationController pushViewController:taskCenter animated:YES];

        
    }
    
    
    DLog(@"每日签到");
    
}





- (void)imviewTaKaifu:(UITapGestureRecognizer *) sender {
    
     UIView *view =  sender.view;
    
    
    
    NSDictionary *dic = self.dataArr[view.tag];
    NSString *urlStr = [NSString stringWithFormat:@"%@",dic[@"url"]];
    NSString *loginStr = [NSString stringWithFormat:@"%@",dic[@"login"]];
    NSString *type = [NSString stringWithFormat:@"%@",dic[@"type"]];
    if ([loginStr isEqualToString:@"1"]) {
        
        
        if ([[HWLocallyStoredData getLoginUserID] isEqualToString:@""]) {
               HWLoginController  *login = [[HWLoginController alloc]init];
                 [self.navigationController pushViewController:login animated:YES];
                return;
         
         }
        
    }
    
    
    
    
     if (![urlStr isEqualToString:@""]) {
            
         
     
         
         if (view.tag == 5 ) {
             
         } else {
             
             XMGWebViewController *webVc = [[XMGWebViewController alloc] init];
                  webVc.url = [NSURL URLWithString:urlStr];
                  [self.navigationController pushViewController:webVc animated:YES];
                   
             return;
         }
         
        
         
     }
    
    
    
    if ([type integerValue] == 1) {
        
               HWPaiHangBangViewController  *PaiHang = [[HWPaiHangBangViewController alloc]init];
               [self.navigationController pushViewController:PaiHang animated:YES];
        return;
        
               
    } else if ([type integerValue] == 2) {
        
        HWLiBaoCenterViewController  *liBao = [[HWLiBaoCenterViewController alloc]init];
             [self.navigationController pushViewController:liBao animated:YES];

        
        HWGiftBagCenterzhongxingViewController *gif = [[HWGiftBagCenterzhongxingViewController alloc]init];
        [self.navigationController pushViewController:gif animated:YES];
        
             
             
        return;
        
    }else if ([type integerValue] == 3) {
        
        
        return;
        
    }else if ([type integerValue] == 4) {
        
        HWOpenClothingViewController  *OpenClothing = [[HWOpenClothingViewController alloc]init];
              [self.navigationController pushViewController:OpenClothing animated:YES];
              
        return;
        
    }else if ([type integerValue] == 5) {
        
        
             HWInviteFriendsViewController *invitefriend = [[HWInviteFriendsViewController alloc]init];
             [self.navigationController pushViewController:invitefriend animated:YES];
             
        return;
        
    }else if ([type integerValue] == 6) {
        
        
        return;
        
    }
    
    
    
    return;
    
    
    
    
    
    
   
//    if (view.tag == 0 ) {
//
////        HWLoginController *login = [[HWLoginController alloc]init];
////        [self.navigationController pushViewController:login animated:YES];
//
//
//
//
//
//
//    } else if (view.tag  == 1 ) {
//
//
//        HWPaiHangBangViewController  *PaiHang = [[HWPaiHangBangViewController alloc]init];
//        [self.navigationController pushViewController:PaiHang animated:YES];
//
//
//
//    }else if (view.tag  == 2 ) {
//
//
//        HWLiBaoCenterViewController  *liBao = [[HWLiBaoCenterViewController alloc]init];
//        [self.navigationController pushViewController:liBao animated:YES];
//
//
//
//    }else if (view.tag  == 3 ) {
//
////        HWCustomerServiceCenterViewController *CustomerService = [[HWCustomerServiceCenterViewController alloc]init];
////        [self.navigationController pushViewController:CustomerService animated:YES];
//
//
//
//    }else if (view.tag  == 4 ) {
//        HWOpenClothingViewController  *OpenClothing = [[HWOpenClothingViewController alloc]init];
//        [self.navigationController pushViewController:OpenClothing animated:YES];
//
//
//    }else if (view.tag  == 5 ) {
//
//
//        HWInviteFriendsViewController *invitefriend = [[HWInviteFriendsViewController alloc]init];
//        [self.navigationController pushViewController:invitefriend animated:YES];
//
//
//
//    } else {
//
//
//    }
    
    
    
    
}




#pragma mark ------ UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return  0;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [[UITableViewCell alloc]init];
    return  cell;
    
    
}









@end
